from fastapi import FastAPI
import uvicorn

app = FastAPI()
@app.get("/date/{dates}")
def root(dates):
    result = is_check(dates)
    return result

# 功能函数
def is_check(date):
    transformDate = {
        'january': 1,
        'february': 2,
        'march': 3,
        'april': 4,
        'may': 5,
        'june': 6,
        'july': 7,
        'august': 8,
        'september': 9,
        'october ': 10,
        'november': 11,
        'december': 12,
        'jan': 1,
        'feb': 2,
        'mar': 3,
        'apr': 4,
        'mei': 5,
        'jun': 6,
        'jul': 7,
        'aug': 8,
        'sept': 9,
        'oct': 10,
        'nov': 11,
        'dec': 12
    }
    transformweek={
        'monday':1,'tuesday':2,'wednesday':3,'thursday':4,'friday':5,'saturday':6,'sunday':7,
        'mon':1,'tue':2,'wed':3,'thu':4,'sri':5,'sat':6,'sun':7
    }
    # reg = '((?:(?:[2468][048]00|[13579][26]00|[1-9]\d0[48]|[1-9]\d[2468][048]|[1-9]\
    # d[13579][26])/(?:0?2/(?:0[1-9]|0?[1-9](?=\D)|[12]\d)))|(?:(?:[12]\d{3})/(?:(?:0?2/' \
    #       '(?:0[1-9]|0?[1-9](?=\D)|1\d|2[0-8]))|(?:0?[3578]/(?:0[1-9]|0?[1-9](?=\D)' \
    #       '|[12]\d|3[01]))|(?:0?[469]/(?:0[1-9]|0?[1-9](?=\D)|[12]\d|30))|(?:1[02]/(' \
    #       '?:0[1-9]|0?[1-9](?=\D)|[12]\d|3[01]))|(?:11/(?:0[1-9]|0?[1-9](?=\D)|[12]\d|3' \
    #       '0))|(?:0?1/(?:0[1-9]|0?[1-9](?=\D)|[12]\d|3[01]}|))))|(\d{4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{1,2}:\d{1,2})|(\d{4}/\d{2}/\d{2}))'  # 时间格式

    # 判断date是否合法
    # an = re.search(reg, date)
    if ' ' not in date:
        if '/' in date:
            split_string = '/'
        elif '-' in date:
            split_string = '-'
        year, month, day = date.split(split_string)

    else:
        date = date.replace(',', ' ').replace('  ', ' ')
        date = date.split(' ')
        year, month, day,week = 0, 0, 0,-1
        t = 'None'
        hour, minute, second = -1, -1, -1
        for info in date:
            if '/' in info:
                year, month, day = info.split('/')
            elif '-' in info:
                year, month, day = info.split('-')
            elif info.lower() in transformDate:
                month = transformDate[info.lower()]
            elif info.lower() in transformweek:
                week = transformweek[info.lower()]
            elif ':' in info:
                info = info.split(':')
                if len(info) == 1:
                    hour = int(info[0])
                elif len(info) == 2:
                    hour, second = int(info[0]), int(info[1])
                elif len(info) == 3:
                    hour, minute, second = int(info[0]), int(info[1]), int(info[2])
                else:
                    return '输入非法'
            elif info.lower() in ['am', 'pm']:
                t = info.lower()
            else:
                if int(info) > 0 and int(info) <= 31:
                    day = int(info)
                elif int(info) > 1000:
                    year = int(info)
        if year == 0 or month == 0 or day == 0:
            return '输入非法'
        if t == 'None':
            if hour != -1:
                if hour < -1 or hour > 23:
                    return '输入非法'
            if minute != -1:
                if minute < -1 or minute > 59:
                    return '输入非法'
            if second != -1:
                if second < -1 or second > 59:
                    return '输入非法'
        else:
            if hour != -1:
                if hour < -1 or hour > 11:
                    return '输入非法'
            if minute != -1:
                if minute < -1 or minute > 59:
                    return '输入非法'
            if second != -1:
                if second < -1 or second > 59:
                    return '输入非法'
        week = int(week)
        if week != -1:
            if week < 1 or week > 7:
                return '输入非法'
    year, month, day = int(year), int(month), int(day)
    if year <= 0:
        return '输入非法'
    if month <= 0 or month > 12:
        return '输入非法'
    if day <= 0 or day >= 32:
        return '输入非法'
    if month not in [1, 3, 5, 7, 8, 10, 12] and day == 31:
        return '输入非法'

    if year % 400 != 0:
        if month == 2 and day >= 29:
            return '输入非法'

    return '输入合法'
if __name__ == '__main__':
    uvicorn.run(app)